/*
 * Copyright 2012 Raimon Rodriguez
 * 
 * This file is part of sgfx.
 * 
 * sgfx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * sgfx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with sgfx. If not, see <http://www.gnu.org/licenses/>.
 */

package com.googlecode.sgfx.base;

import javax.swing.JComboBox;

/**
 * Utility class for the {@code JComboBox} Swing component. It provides utility methods to reduce 
 * boiler-plate code and/or make code easier to read when dealing with this component.
 * 
 * @author raimon.rodriguez
 */
public final class ComboBoxes {
    
    private ComboBoxes() {}
    
    /**
     * Clears the selection from the given {@code JComboBox}. This method is equivalent to but makes 
     * code more readable than {@code pComboBox.setSelectedItem(null)} or
     * {@code pComboBox.setSelectedIndex(-1)}.
     * 
     * @param pComboBox the {@code JComboBox} to clear the selection from.
     * @throws NullPointerException if the parameter is {@code null}.
     */
    public static void clearSelection(JComboBox pComboBox) {
        // chosen this method over setSelectedIndex(-1) as the mentioned method ends up calling the
        // setSelectedItem(null) if the index is -1, so we are saving one extra method call
        pComboBox.setSelectedItem(null);
    }
    
    /**
     * Checks whether there's an item selected in the given {@code JComboBox}. Returns {@code true}
     * if there's one, otherwise returns {@code false}. This method is equivalent to but makes code
     * more readable than {@code pComboBox.getSelectedItem() == null} or
     * {@code pComboBox.gettSelectedIndex() == -1}.
     * 
     * @param pComboBox the combo box to check if there's an item selected.
     * @return {@code true} if there's a selected item in the specified {@code JComboBox},
     * otherwise {@code false}
     * @throws NullPointerException if the parameter is {@code null}.
     */
    public static boolean isSelectionEmpty(JComboBox pComboBox) {
        return pComboBox.getSelectedItem() == null;
    }
}